import { useEffect, useRef, useState } from 'react';

function useRefTest() {
  const box = useRef(); 
  const [ count, setCount ] = useState(0);
  let timerId = useRef();
  useEffect(() => {
    timerId.current = setInterval(() => {
      setCount(count => count + 1);
    }, 1000);
  }, []);
  const stopCount = () => {
    clearInterval(timerId.current);
  }
  return (
    <div ref={box}>
      <button onClick={()=>console.log(box)}>获取DIV</button>
      <span>{count}</span>
      <button onClick={stopCount}>停止计时</button>
    </div>
  )
}

export default useRefTest;